package com.real;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.entity.Word;
import com.util.Function;
import com.util.Internet;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: real
 * Date: 12-7-17
 * Time: 上午10:33
 * To change this template use File | Settings | File Templates.
 */
public class StudyWordActivity extends Activity {

    int index = 0;
    List<Word> words = null;
    TextView progress, word, phonetic, meanings;
    Button details_button_prev, details_button_next;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.study_word);

        words = PushedWordsActivity.getWords();

        progress = (TextView) findViewById(R.id.study_progress);
        word = (TextView) findViewById(R.id.details_word_textview);
        phonetic = (TextView) findViewById(R.id.details_phonetic_textview);
        meanings = (TextView) findViewById(R.id.details_meanings_textview);

        details_button_prev = (Button) findViewById(R.id.details_button_prev);
        details_button_next = (Button) findViewById(R.id.details_button_next);

        SwitchToWord(0);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.details_button_prev:
                SwitchToWord(index - 1);
                break;
            case R.id.details_button_next:
                SwitchToWord(index + 1);
                break;
        }
    }

    public void SwitchToWord(int i) {
        if (i >= 0 && i <= words.size() - 1) {
            index = i;
            ShowWord(i);

            details_button_prev.setVisibility(index == 0 ?
                    View.INVISIBLE : View.VISIBLE);
            // Next始终是Visable
//            details_button_next.setVisibility(index == words.size() - 1 ?
//                    View.INVISIBLE : View.VISIBLE);
        }

        if(i == words.size()){
            Function.SwitchActivity(this, ReadyOrNotActivity.class);
        }

    }

    public void ShowWord(int i) {
        Word w = words.get(i);

        progress.setText(i + 1 + "/" + words.size());
        word.setText(w.getword());
        phonetic.setText(w.getphonetic());
        meanings.setText(w.getexplain0());
    }


}
